from django.db import models
from db.abstract_model import *
from tinymce.models import HTMLField
from utils.wrapper import *

# 菜品分类模型管理器
class DishClassifyManager(models.Manager):

    # 从数据库中获取菜品分类
    def get_dish_classify(self):
        try:
            dish_classify = DishClassify.objects.all()
            return dish_classify
        except:
            pass


# 菜品分类模型
class DishClassify(AbstractModel):

    # 品种
    classify = models.CharField(max_length=20,verbose_name="菜品分类")
    # 英文名
    en_classify = models.CharField(max_length=40,default="",verbose_name="英文名")
    # 分类图片
    classify_img = models.ImageField(default="",verbose_name="分类标题图片")
    # 分类简述
    classify_desc = models.CharField(max_length=100,default="",verbose_name="分类简述")

    objects = DishClassifyManager()

    def __str__(self):
        return self.classify

    # admin管理中指定名字
    class Meta:
        verbose_name = "菜品分类"
        verbose_name_plural = "菜品分类"



# 菜品模型管理器
class DishesManager(models.Manager):

    # 从数据库获取菜品分类后的菜品对象
    def get_dishes(self,request):
        # 获取分类索引
        index = get(request, "index", "1")
        try:
            # 获取类名
            classify = DishClassify.objects.get(id=index).classify
            dishes = Dishes.objects.filter(dish_type_id=index)
            dishes.classify_name = classify
            return dishes
        except:
            pass

    # 获取一个菜品
    def get_one_dish(self,request):
        try:
            dish_id = get(request, "id")
            dish = Dishes.objects.get(id=dish_id)
            return dish
        except:
            pass


    # 获取最热门点赞四个菜品
    def get_dish_hot_4(self):
        try:
            return self.all().order_by("-dish_praise")[:4]
        except:
            pass

    # 更改菜品评论数
    def update_dish_response_num(self, request):
        # 根据id获取菜品
        dish_id = post(request, "dish_id")
        dish = self.get(id=dish_id)
        dish.dish_response += 1
        dish.save()

    # 更新点赞数
    def update_dish_praise(self,request):
        # 根据id获取菜品
        try:
            dish_id = post(request, "dish_id")
            dish = self.get(id=dish_id)
            dish.dish_praise += 1
            dish.save()
            return dish.dish_praise
        except:
            pass



# 菜品模型
class Dishes(AbstractModel):
    # 菜名
    dish_name = models.CharField(max_length=30,verbose_name="菜名")
    # 菜品类型
    dish_type = models.ForeignKey(DishClassify,verbose_name="分类")
    # 价格
    dish_price = models.DecimalField(max_digits=5,decimal_places=2,verbose_name="价格")
    # 菜品图片
    dish_img = models.ImageField(upload_to="images/dishes/",verbose_name="图片")
    # 菜品简介
    dish_short = models.CharField(max_length=50,verbose_name="简介")
    # 菜品文字介绍
    dish_desc = HTMLField(verbose_name="详情")
    # 菜品点赞数
    dish_praise = models.IntegerField(default=0,verbose_name="点赞术")
    # 菜品评论数
    dish_response = models.IntegerField(default=0,verbose_name="评论数")

    objects = DishesManager()

    def __str__(self):
        return self.dish_name


    # admin管理中指定名字
    class Meta:
        verbose_name = "菜品"
        verbose_name_plural = "菜品"


class DishResponseManager(models.Manager):
    # 将评论内容存入数据库
    # 更改菜品评论数
    def save_response(self,request):
        user_name = post(request, "user_name")
        response = post(request, "response")
        dish_id = post(request, "dish_id")
        dish_re = DishResponse()
        dish_re.response_dish_id = dish_id
        dish_re.user_name = user_name
        dish_re.response = response
        dish_re.save()


    # 通过id获取5条所有评论
    def get_5_response(self,dish_id,re_ok_num):
        try:
            return self.filter(response_dish_id=dish_id).order_by("-id")[int(re_ok_num):int(re_ok_num)+5]
        except:
            pass


class DishResponse(AbstractModel):
    # 用户名
    user_name = models.CharField(max_length=20,verbose_name="客户名称")
    # 评论内容
    response = models.CharField(max_length=200,verbose_name="评论内容")
    # 评论的菜品
    response_dish = models.ForeignKey(Dishes,verbose_name="评论菜品")

    objects = DishResponseManager()

    # admin管理中指定名字
    class Meta:
        verbose_name = "菜品评价"
        verbose_name_plural = "菜品评价"



